package com.futurelight.recipes.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.futurelight.recipes.R;
import com.futurelight.recipes.model.Result;

import java.util.List;

public class ShimmerAdapter extends RecyclerView.Adapter<ShimmerAdapter.MyViewHolder> {
    private List<Result> results = null;

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater from = LayoutInflater.from(parent.getContext());
        View inflate = from.inflate(R.layout.item_food_recipes_info, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Result result = results.get(position);
        holder.setResult(result);
    }

    @Override
    public int getItemCount() {
        if (results == null) {
            return 0;
        }
        return results.size();
    }

    public void setResults(List<Result> results) {
        this.results = results;
        notifyDataSetChanged();
    }


    private CallBackResult callback;

    public void setCallback(CallBackResult callback) {
        this.callback = callback;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        View itemViewRoot;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            itemViewRoot = itemView;
        }

        public void setResult(Result result) {
            ImageView recipeImage = itemViewRoot.findViewById(R.id.recipe_image);
            TextView title = itemViewRoot.findViewById(R.id.detail_title);
            TextView like = itemViewRoot.findViewById(R.id.like);
            TextView timer = itemViewRoot.findViewById(R.id.timer);
            Glide.with(itemViewRoot).load(result.getImage()).error(R.drawable.plate).into(recipeImage);

            title.setText(result.getTitle());
            like.setText(String.valueOf(result.getAggregateLikes()));
            timer.setText(String.valueOf(result.getReadyInMinutes()));

            itemViewRoot.setOnClickListener(new View.OnClickListener() {
                                                                                  @Override
                                                                                  public void onClick(View v) {
                                                                                      callback.callBackResult(result);
                                                                                  }
                                                                              }
            );
        }
    }
}
